﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using HF.Architecture.Data;
//using HF.WorkFlow.Const;
using YYWebControl.YYSimpleCalendar;
using ZX.WebApp.CrmWorkFlowService;

namespace ZX.WebApp
{
    
    /// <summary>
    /// BaseUserControl 的摘要说明
    /// 每个用户控件必须处理好三种情况，1新建，2修改，3查看
    /// </summary>
    public  class BaseUserControl : System.Web.UI.UserControl, IUserCtrlInterface
    {
        private string workflowId = "";
        private string worktaskId = "";
        private string workflowInsId = "";
        private string worktaskInsId = "";
        private string operatorInsId = "";
        private string userId = "";
        private string userName = "";
        private string ctrlState = "";
        private string pageState = "";
        private string dutyCaption = "";
        public string archCaption = "";
        private bool isDraft = false;
        /// <summary>
        /// 工作流模板Id
        /// </summary>
        public virtual string WorkflowId
        {
            get
            {
                return this.workflowId;
            }
            set
            {
                this.workflowId = value;
            }
        }
        /// <summary>
        /// 任务模板Id
        /// </summary>
        public virtual string WorktaskId
        {
            get
            {
                return this.worktaskId;
            }
            set
            {
                this.worktaskId = value;
            }
        }
        /// <summary>
        /// 工作流实例Id
        /// </summary>
        public virtual string WorkflowInsId
        {
            get
            {
                return this.workflowInsId;
            }
            set
            {
                this.workflowInsId = value;
            }
        }
        /// <summary>
        /// 任务实例Id
        /// </summary>
        public virtual string WorktaskInsId
        {
            get
            {
                return this.worktaskInsId;
            }
            set
            {
                this.worktaskInsId = value;
            }
        }
        /// <summary>
        /// 操作实例Id
        /// </summary>
        public virtual string OperatorInsId
        {
            get
            {
                return this.operatorInsId;
            }
            set
            {
                this.operatorInsId = value;
            }
        }
        /// <summary>
        /// 用户Id
        /// </summary>
        public virtual string UserId
        {
            get
            {
                return this.userId;
            }
            set
            {
                this.userId = value;
            }
        }
        /// <summary>
        /// 用户名
        /// </summary>
        public virtual string UserName
        {
            get
            {
                return this.userName;
            }
            set
            {
                this.userName = value;
            }
        }
        /// <summary>
        /// 职务
        /// </summary>
        public virtual string DutyCaption
        {
            get
            {
                return this.dutyCaption;
            }
            set
            {
                this.dutyCaption = value;
            }
        }
        /// <summary>
        /// 部门
        /// </summary>
        public virtual string ArchCaption
        {
            get
            {
                return this.archCaption;
            }
            set
            {
                this.archCaption = value;
            }
        }
        /// <summary>
        /// 用户控件的状态,新建，修改，查看
        /// </summary>
        public virtual string CtrlState
        {
            get
            {
                return this.ctrlState;
            }
            set
            {
                this.ctrlState = value;
            }
        }
        /// <summary>
        /// 表单状态,新建，修改，查看
        /// </summary>
        public virtual string PageState
        {
            get
            {
                return this.pageState;
            }
            set
            {
                this.pageState = value;
            }
        }
        /// <summary>
        /// 草稿
        /// </summary>
        public virtual bool IsDraft
        {
            get
            {
                return this.isDraft;
            }
            set
            {
                this.isDraft = value;
            }
        }
        public BaseUserControl()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 初始化流程信息
        /// </summary>
        public virtual void Page_Load()
        {

            userId = this.Page.User.Identity.Name; 
            //DataTable dt = ArchitectureData.GetInstance().GetArchitecturesOfUser(userId);
            ZX.WebApp.CrmArchitectureService.ArchitectureServiceClient aclient=new ZX.WebApp.CrmArchitectureService.ArchitectureServiceClient();
            DataTable dt = aclient.GetArchitecturesOfUser(userId);
            if (dt != null && dt.Rows.Count > 0)
            {
                userName = dt.Rows[0]["UserName"].ToString();
                dutyCaption = dt.Rows[0]["DutyCaption"].ToString();
                archCaption = dt.Rows[0]["Caption"].ToString();
            }
             
        }
        public void setReadOnly(Control ctrl)
        {
            if (ctrlState == WorkFlowConst.STATE_VIEW)
            {
                string ctrlTypeName = "";
                foreach (Control c in ctrl.Controls)
                {
                    ctrlTypeName = c.GetType().Name;
                    switch (ctrlTypeName)
                    {
                        case "Button": 
                            (c as Button).Enabled = false;
                            break;
                        case "DropDownList":
                            (c as DropDownList).Enabled = false;
                            break;
                        case "TextBox":
                            (c as TextBox).ReadOnly = true;
                            break;
                        case "YYSimpleCalendar":
                            (c as YYSimpleCalendar).ReadOnly = true;
                            break;
                    }
                    if (c.HasControls())
                        setReadOnly(c);
                }
            }
        }
        public virtual void InitUserControl()
        {
            setReadOnly(this);
        }
        
        public virtual void SaveUserControl(bool isDraft)
        {
            this.isDraft = isDraft;
            
        }
    }
}
